package com.longe.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @SentinelResource(value = "test-sentinel", blockHandler = "consumerBlockHandler",fallback = "consumerFallback")
    @RequestMapping(value = "consumer/{id}")
    public String consumer(@PathVariable(value = "id") Integer id){
        if(id != 1){
            throw new RuntimeException("error happen ......");
        }
        return restTemplate.getForObject("http://provider-server/provider", String.class);
    }

    public String consumerBlockHandler(Integer id, BlockException ex) {
		return "触发 [Sentinel配置的相关规则]";
	}

	public String consumerFallback(Integer id) {
		return "触发 [异常兜底,不好意思,网络出现故障了哟]";
	}
}